using System; 
using System.Text; 
using System.Data;
using System.IO;
using Kcitwm.DataHelper;
using WQFree.Interfaces; 

namespace WQFree.DataAccess
{


    public class CommonDAO : ICommon 
    {
         

        DataSet ICommon.GetDataSet(string cmdText, CommandType cmdType, string tableName)
        {
            CommonHelper hp =Helper.Instance();
            return hp.ExcuteDataSet(cmdText, cmdType, tableName);
        }

        IDataReader ICommon.GetDataReader(string cmdText, CommandType cmdType)
        {
           CommonHelper hp =Helper.Instance();
            return hp.ExcuteDataReader(cmdText, cmdType);
        }

        Object[] ICommon.GetFields(string query, CommandType cmdType)
        {
           CommonHelper hp =Helper.Instance();
             IDataReader dr=hp.ExcuteDataReader(query, cmdType);
             if (dr.Read())
             {
                 object[] objs=new object[dr.FieldCount];
                 dr.GetValues(objs);
                 dr.Close();
                 return objs;
             }
             return null;
        }

        object ICommon.ExecuteScalar(string cmdText, CommandType cmdType)
        {
           CommonHelper hp =Helper.Instance();
            return hp.ExecuteScalar(cmdText, cmdType);
        }

        int ICommon.ExecuteNonQuery(string cmdText, CommandType cmdType)
        {
           CommonHelper hp =Helper.Instance();
            return hp.ExecuteNonQuery(cmdText, cmdType);
        }

        DataSet ICommon.GetPagedDataSet(string tableName, string selectField, string idFileld, string where, string sortField, int sortType, int pageIndex, int pageSize, bool isCount, out int count)
        {
            if (pageIndex < 1)
                pageIndex = 1;
           CommonHelper hp =Helper.Instance();
            hp.AddParameters("TableName", tableName);
            hp.AddParameters("SelectField",selectField);
            hp.AddParameters("IDField",idFileld);
            hp.AddParameters("Where",where);
            hp.AddParameters("SortField",sortField);
            hp.AddParameters("SortType",sortType);
            hp.AddParameters("PageIndex",pageIndex);
            hp.AddParameters("PageSize",pageSize);
            hp.AddParameters("IsCount",isCount);
            IDbDataParameter p = hp.AddParameters("RecordCount", DbType.Int32,4);
            p.Direction = ParameterDirection.Output;
            DataSet ds = hp.ExcuteDataSet("PagedRecordSP", CommandType.StoredProcedure, "PagedData");
            count = 0;
            if (p != null && p.Value != null)
            {
                if (p.Value.ToString() == "")
                    count = 0;
                else
                    int.TryParse(p.Value.ToString(), out count);
            }
            return ds;
        }
        IDataReader ICommon.GetPagedDataReader(string tableName, string selectField, string idFileld, string where, string sortField, int sortType, int pageIndex, int pageSize, bool isCount, out int count)
        {
           CommonHelper hp =Helper.Instance();
            hp.AddParameters("TableName", tableName);
            hp.AddParameters("SelectField",selectField);
            hp.AddParameters("IDField",idFileld);
            hp.AddParameters("Where",where);
            hp.AddParameters("SortField",sortField);
            hp.AddParameters("sortType",sortType);
            hp.AddParameters("PageIndex",pageIndex);
            hp.AddParameters("PageSize",pageSize);
            hp.AddParameters("IsCount",isCount);
            IDbDataParameter p = hp.AddParameters("RecordCount", DbType.Int32,4);
            p.Direction = ParameterDirection.Output;
            IDataReader dr = hp.ExcuteDataReader("PagedRecordSP", CommandType.StoredProcedure);
            count = null==p.Value?0:(int)p.Value;
            return dr;
        }
        
        int     ICommon.DeleteRecords(string tableName, string idField, string ids)
        {
           CommonHelper hp =Helper.Instance();
            hp.AddParameters("TableName", tableName);
            hp.AddParameters("IDField", idField);
            hp.AddParameters("IDs", ids);
            return hp.ExecuteNonQuery("DeleteRecordsByIdsSP", CommandType.StoredProcedure);
        }
        
    }
}
